package com.example.main;

import com.example.mattrigle.ControlApp;
import com.example.mattrigle.GameCanvas;
import com.example.mattrigle.MainActivity;
import com.example.mattrigle.R;
import com.example.mattrigle.Sound.SoundGame;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class Menu_Activity extends Activity implements OnClickListener{
	private GameCanvas gameView;
	FrameLayout lay_anime;
	Game_Dialog gameDialog;
	ImageView sound;
	private Boolean isFinishAnimate = true;
//	private SoundGame soundGame;
	public static int on = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_menu);
		on = 1;
		lay_anime = (FrameLayout) findViewById(R.id.layout);
		sound = (ImageView) findViewById(R.id.sound2);
		ImageView user = (ImageView) findViewById(R.id.users2);
		ImageView btn_play = (ImageView) findViewById(R.id.btn_play2);
		
		sound.setOnClickListener(this);
		user.setOnClickListener(this);
		btn_play.setOnClickListener(this);
		
		//startActivity(this,MainActivity.class);
		
		gameDialog = new Game_Dialog();
		animet();
		
		if(ControlApp.Username.equals(""))
		{
			gameDialog.dialog_user(Menu_Activity.this,true);

		}
//		soundGame = new SoundGame();
	     FrameLayout btnExit = (FrameLayout)findViewById(R.id.btnExit);
	     btnExit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
	}
	@Override
	protected void onResume() {
		if(on == 1){
			SoundGame.getInstance().playSoundMenu(getApplicationContext());
		}		
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		SoundGame.getInstance().stopSound();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_play2:
			 ControlApp.Unlock45=true;
			 ControlApp.Unlock30=false;
			 ControlApp.Unlock60=false;
			SoundGame.getInstance().playSoundButton(Menu_Activity.this);
			gameDialog.dialog_degree(this);
			
			
			break;
		case R.id.sound2:
			if(on == 1){
				SoundGame.getInstance().playSoundButton(Menu_Activity.this);
				sound.setImageDrawable(getResources().getDrawable(R.drawable.btn_soundon));
				SoundGame.getInstance().stopSound();
				on = 0;
			}else{
				SoundGame.getInstance().playSoundButton(Menu_Activity.this);
				sound.setImageDrawable(getResources().getDrawable(R.drawable.btn_soundoff));
				SoundGame.getInstance().playSoundMenu(getApplicationContext());
				on = 1;
			}			
			break;
		case R.id.users2:
			SoundGame.getInstance().playSoundButton(Menu_Activity.this);
			gameDialog.dialog_user(Menu_Activity.this,false);
			break;
		}
	}
	
	
	public float angle_0 = 0;
	public float angle_90 = 90;
	public float angle_count = 0;
	
	private void animet(){
		gameView = new GameCanvas(getApplicationContext(), true);
		gameView.setLineWidth(10);
		gameView.setScale(0.9f);
		gameView.setHideHand(false);
		//gameView.newStageWithAngle(true, 0);
		lay_anime.addView(gameView, new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));

		isFinishAnimate = false;
		hand1();
	}
	
	public void hand1(){
		final Handler handler1 = new Handler();
		handler1.post(new Runnable() {
			
			@Override
			public void run() {
				if(isFinishAnimate)return;
				gameView.newStageWithAngle(true, angle_count);
				angle_count = angle_count + 0.5f;
				if(angle_count >= angle_90 ){
					hand2();
				}else {
					handler1.postDelayed(this, 1);
				}
				
			}
		});
	}
	
	@Override
	public void finish() {
		isFinishAnimate = true;
		Log.e("Math", "Finish Menu:");
		
		super.finish();
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	public void hand2(){
		final Handler handler2 = new Handler();
		handler2.post(new Runnable() {
			
			@Override
			public void run() {
				if(isFinishAnimate)return;
				gameView.newStageWithAngle(true, angle_count);
				angle_count = angle_count - 0.5f;
				if(angle_count <= angle_0){
					hand1();
				}else{
					handler2.postDelayed(this, 1);
				}
				
			}
		});
		
	}
	
}
